package com.hourse.service;

import com.hourse.dao.UserDao;
import com.hourse.pojo.TbUser;
import com.hourse.util.IdWorker;
import com.hourse.util.JwtUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder encoder;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtUtil jwtUtil;


    public List<TbUser> userList(){
        String token = (String) request.getAttribute("claims_admin");
        if (token==null||"".equals(token)){
            throw new RuntimeException("权限不足");
        }
        return userDao.findAll();
    }

    public TbUser findById(Long id) {
        return userDao.findById(id).get();
    }

    public String sendSms(String mobile) {
        //生成六位随机数
        String random = RandomStringUtils.randomNumeric(6);
        //向缓存中放一份
        redisTemplate.opsForValue().set("yzm"+mobile,random, 1,TimeUnit.MINUTES);
        //给用户发一份
        Map<String,String> map=new HashMap<>();
        map.put("mobile",mobile);
        map.put("random",random);
        rabbitTemplate.convertAndSend("sms",map);
        //在控制台显示一份（【方便测试】）

        System.out.println("验证码为:"+random);
        return random;
    }

    public Object huoqvYzn(String mobile) {
        return redisTemplate.opsForValue().get("yzm"+mobile);
    }

    public void add(TbUser user) {
        user.setId(idWorker.nextId());
        //密码加密
        user.setPassword(encoder.encode(user.getPassword()));
        userDao.save(user);
    }




    public TbUser login(String phone, String password) {
        TbUser user = userDao.findByPhone(phone);
        if (user!=null&&encoder.matches(password,user.getPassword())){
            return user;
        }
        return null;
    }


    public void deleteById(Long id) {
     /*   String header = request.getHeader("Authorization");
        if (header==null||"".equals(header)){
            throw new RuntimeException("权限不足!");
        }
        if (!header.startsWith("Bearer ")){
            throw new RuntimeException("权限不足!");
        }
        //得到令牌
        String token=header.substring(7);
        try {
            Claims claims = jwtUtil.parseJWT(token);
            String roles = (String) claims.get("roles");
            if (roles==null||!roles.equals("admin")){
                throw new RuntimeException("权限不足!");
            }
        }catch (Exception e){
            throw new RuntimeException("权限不足!");
        }*/
        String token = (String) request.getAttribute("claims_admin");
        if (token==null||"".equals(token)){
            throw new RuntimeException("权限不足");
        }
        userDao.deleteById(id);
    }
}
